package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 12629
 * @Date 2022/4/15 17:41
 * @Description：
 */
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Resource
    private LoveMusicMapper loveMusicMapper;

    /**
     * 收藏音乐
     * @param id        传入相关音乐的id
     * @param request
     * @return
     */
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request) {

        // 前端转入的字符串，这里转化一下
        int musicId = Integer.parseInt(id);
        System.out.println("musicId："+musicId);
        // 取出用户id和音乐id
        HttpSession httpSession = request.getSession(false);
        User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId："+userId);

        // 先查询一下指定歌曲是否被当前用户收藏过
        Music music = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId,musicId);
        if(music != null) {
            //之前收藏过，不能进行收藏了!TODO:【作业】加一个取消收藏的功能！
            return new ResponseBodyMessage<>(-1,"您之前收藏过这个音乐",false);
        }

        // 再次收藏
        boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);
        // 收藏成功
        if(effect) {
            return new ResponseBodyMessage<>(0,"收藏成功！",true);
        }
        // 收藏失败
        else {
            return new ResponseBodyMessage<>(0,"收藏失败！",true);
        }
    }

    /**
     * 查询收藏列表
     * @param musicName
     * @param request
     * @return
     */
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest  request) {
        //1、拿出当前用户id
        HttpSession httpSession = request.getSession(false);
        User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId："+userId);

        List<Music> musicList = null;
        // 如果传入的音乐名称为空，则全部查询
        if(musicName == null) {
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        }
        // 如果名称不为空，则模糊匹配传入的音乐名称
        else {
            musicList = loveMusicMapper.findLoveMusicBykeyAndUID(musicName,userId);
        }
        // 返回查询到的音乐列表
        return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息",musicList);
    }

    /**
     * 删除收藏音乐
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,HttpServletRequest request) {
        int musicId = Integer.parseInt(id);

        HttpSession httpSession = request.getSession(false);
        User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        // 删除指定用户对应指定收藏的音乐
        int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);

        // 删除成功
        if(ret == 1) {
            return new ResponseBodyMessage<>(0,"取消收藏成功！",true);
        }
        // 删除失败
        else {
            return new ResponseBodyMessage<>(-1,"取消收藏失败！",false);
        }
    }
}
